﻿namespace PrismEx.SampleApp
{
    using System;
    using System.ComponentModel.Composition.Hosting;
    using System.Linq;
    using System.Threading;
    using System.Windows;
    using System.Windows.Threading;

    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Prism.MefExtensions;
    using Microsoft.Practices.Prism.Modularity;

    using PrismEx.Events;
    using PrismEx.Modules.NavigationService;
    using PrismEx.SampleApp.Model;
    using PrismEx.SampleApp.Modules.SplashScreen;

    public class Bootstrapper : MefBootstrapper
    {
        private AutoResetEvent waitForCreation;

        protected override void InitializeShell()
        {
            var shell = this.Shell as Shell;

            if (shell == null) throw new InvalidOperationException("Shell must be created before it is initialized.");

            shell.Show();
        }

        protected override DependencyObject CreateShell()
        {
            return this.Container.GetExportedValue<Shell>();
        }

        protected override void ConfigureAggregateCatalog()
        {
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(NavigationServiceModule).Assembly));
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Shell).Assembly));
        }

        protected override void InitializeModules()
        {
            var splashScreenService = Container.GetExportedValue<SplashScreenService>();
            splashScreenService.Start();
            base.InitializeModules();
            var eventAggregator = Container.GetExportedValue<IEventAggregator>();
            eventAggregator.Publish(new CloseSplashScreenEvent());
        }

        public override void Run(bool runWithDefaultConfiguration)
        {
            base.Run(runWithDefaultConfiguration);
        }
    }
}